<template>
  <div class="jsbos-common-layout">
    <div class="jsbos-common-layout-center jsbos-flex-main">
      <el-row class="jsbos-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="4">
            <el-form-item :label="$t('common.keyword')">
              <el-input
                v-model="listQuery.keyword"
                placeholder="请输入姓名/账号/手机号"
                clearable
                @keyup.enter.native="search()"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-select v-model="audit" placeholder="请选择" clearable>
                <el-option
                  v-for="(item, index) in auditOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()">
                {{ $t("common.search") }}</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >{{ $t("common.reset") }}
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="jsbos-common-layout-main jsbos-flex-main">
        <div class="jsbos-common-head">
          <div></div>
          <div class="jsbos-common-head-right">
            <el-tooltip
              effect="dark"
              :content="$t('common.refresh')"
              placement="top"
            >
              <el-link
                icon="icon-ym icon-ym-Refresh jsbos-common-head-icon"
                :underline="false"
                @click="initData()"
              />
            </el-tooltip>
          </div>
        </div>
        <jsbos-table v-loading="listLoading" :data="tableData">
          <el-table-column
            prop="account"
            label="账户"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column prop="realName" label="姓名" width="100" />
          <el-table-column
            prop="organize"
            label="所属组织"
            min-width="200"
            show-overflow-tooltip
          />
          <el-table-column
            prop="auditResult"
            label="审计结果"
            min-width="300"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="creatorTime"
            label="创建时间"
            :formatter="jsbos.tableDateFormat"
            width="120"
          />
          <el-table-column
            prop="changePasswordDate"
            label="密码修改时间"
            :formatter="jsbos.tableDateFormat"
            width="120"
          />
          <el-table-column
            prop="lastLogTime"
            label="最后登录时间"
            :formatter="jsbos.tableDateFormat"
            width="120"
          />
          <el-table-column
            prop="enabledMark"
            label="状态"
            width="70"
            align="center"
          >
            <template slot-scope="scope">
              <el-tag
                type="success"
                disable-transitions
                v-if="scope.row.enabledMark == 1"
                >正常
              </el-tag>
              <el-tag
                type="warning"
                disable-transitions
                v-else-if="scope.row.enabledMark == 2"
                >锁定
              </el-tag>
              <el-tag type="danger" disable-transitions v-else>禁用</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template slot-scope="scope" v-if="!scope.row.isAdministrator">
              <tableOpts
                @edit="addOrUpdateHandle(scope.row.id)"
                @del="handleDel(scope.row.id)"
              >
              </tableOpts>
            </template>
          </el-table-column>
        </jsbos-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
        <RolePreview
          v-if="rolePreviewVisible"
          ref="RolePreview"
          @close="removeRolePreview"
        />
      </div>
    </div>
    <Form v-show="formVisible" ref="Form" @close="removeForm" />
  </div>
</template>
<script>
import { getDepartmentSelector } from "@/api/permission/department";
import { getAuditList, delUser } from "@/api/permission/user";
import Form from "@/views/permission/user/Form";

export default {
  components: {
    Form,
  },
  data() {
    return {
      keyword: "",
      treeData: [],
      tableData: [],
      treeLoading: false,
      listLoading: true,
      listQuery: {
        organizeId: "",
        keyword: "",
        currentPage: 1,
        pageSize: 20,
      },
      defaultProps: {
        children: "children",
        label: "fullName",
      },
      total: 0,
      type: "",
      formVisible: false,
      diagramVisible: false,
      resetFormVisible: false,
      rolePreviewVisible: false,
      importFormVisible: false,
      exportFormVisible: false,
      expands: true,
      refreshTree: true,
      filterText: "",
      auditOptions: [
        {
          fullName: "未修改初始密码",
          id: "1",
        },
        {
          fullName: "定期未修改密码",
          id: "2",
        },
        {
          fullName: "长期未登录",
          id: "3",
        },
        {
          fullName: "用户部门不存在",
          id: "4",
        },
        {
          fullName: "用户角色不存在",
          id: "5",
        },
        {
          fullName: "用户部门关系不存在",
          id: "6",
        },
        {
          fullName: "用户角色关系不存在/异常",
          id: "7",
        },
        {
          fullName: "未关联第三方账户数据",
          id: "8",
        },
        {
          fullName: "手机号重复",
          id: "9",
        },
        {
          fullName: "邮箱重复",
          id: "10",
        },
      ],
      audit: "",
    };
  },
  watch: {
    filterText(val) {
      this.$refs.treeBox.filter(val);
    },
  },
  created() {
    this.getOrganizeList(true);
  },
  methods: {
    search() {
      this.listQuery.currentPage = 1;
      this.listQuery.pageSize = 20;
      this.listQuery.sort = "desc";
      this.initData();
    },
    reset() {
      this.listQuery.keyword = "";
      this.search();
    },

    filterNode(value, data) {
      if (!value) return true;
      return data.fullName.indexOf(value) !== -1;
    },
    getOrganizeList(isInit) {
      this.treeLoading = true;
      getDepartmentSelector()
        .then((res) => {
          this.treeData = res.data.list;
          this.treeLoading = false;
          if (isInit) this.initData();
        })
        .catch(() => {
          this.treeLoading = false;
        });
    },
    initData() {
      this.listLoading = true;
      let param = {
        ...this.listQuery,
        audit: this.audit,
      };
      getAuditList(param)
        .then((res) => {
          this.tableData = res.data.list;
          this.total = res.data.pagination.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    handleNodeClick(data) {
      if (this.listQuery.organizeId === data.id) return;
      this.listQuery.organizeId = data.id;
      this.type = data.type;
      this.reset();
    },
    addOrUpdateHandle(id) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.Form.init(
          id,
          this.type === "department" ? this.listQuery.organizeId : ""
        );
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          delUser(id).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              duration: 1500,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    removeForm(isRefresh) {
      this.formVisible = false;
      if (isRefresh) {
        this.keyword = "";
        this.initData();
      }
    },
  },
};
</script>
